/**
 * Created by yangfan on 2016/12/28.
 */
/**
 * Created by Administrator on 2016/8/11.
 */
define(["require",'config/appregister',"business/vedio/services/httpServices","business/vedio/dir/http","business/workSkill/directive/startLevel"],function(require,app,httpServices){
    app.controller('searchCourseCtrl',function($scope,$http,$state,$stateParams){
        $scope.updateCollection=function(){
            auth.isLogin().then(function(){
                updateCollection();
            })
        }
        $scope.clearClas=function(){

        }
        /*星级*/
        $scope.$parent.isDetailPage = false;
        $scope.$parent.isMainpage = true;
        $scope.goTo = function(item){
            $state.go("eng.vedioDetails",{courseId:item.courseId})
        }
        $scope.linkSearch=function(name){
            $rootScope.GLOBALKEYWORD =  name;
            sessionStorage.searchKeyWord = name;
            $state.transitionTo("search.course", {}, {
                reload: true, inherit: false, notify: true
            });
        }
        $scope.max = 5;//最大星级
        $scope.ratingVal = 1;///HTML中也可传递(优先于js),默认星级，必需设置，下面同样名字覆盖，否则不显示；$
        $scope.readonly = true;//false可编辑，true不可编辑
        $scope.onHover = function(val){
            $scope.hoverVal = val;
        };
        $scope.onLeave = function(){
            $scope.hoverVal = null;
        }
        $scope.onChange = function(val){
            $scope.ratingVal = val;
        };
        $scope.listParams = {
            currentTcId: "",
            download: false,
            time: false,
            pageNo: 1,
            pageSize:10,
            all: false,
            typeId: "",
            scId: "",
            dcId: "",
            keyWord:"",
            //dcName scName 用于加入缓存 在详情中显示面包树导航
            dcName:"",
            scName:""
        };
         
        /*tab栏目切换*/
        $scope.tab=-1;
        $scope.color = 2;
        $scope.showCOn=2;
        ONeClass()
        function ONeClass(){
            var req ={
                type:"GET",
                uri:"getTopCategory",
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.CityVideos = response.data;
                    $scope.oneClassId = response.data[0].tcId
                    var req ={
                        type:"GET",
                        uri:"getSecondaryCategory?tcId=" + response.data[0].tcId,
                        data:{}
                    };
                    httpServices.getcourseCata(req).success(function(response){
                        $scope.$apply(function(){
                            $scope.WorkCousrse = response.data.list;
                        })
                    });


                    if(sessionStorage.topId != undefined && sessionStorage.twoId == undefined && sessionStorage.thrId == undefined){
                        $scope.tab = sessionStorage.topId;
                        courseList()
                    }else if(sessionStorage.topId != undefined && sessionStorage.twoId != undefined && sessionStorage.thrId == undefined ){
                        $scope.tab = sessionStorage.topId;
                        $scope.currentId =  sessionStorage.twoId;
                        if(sessionStorage.topId == $scope.CityId){
                            $scope.showCOn=1;
                        }else{
                            $scope.showCOn2=1;
                        }
                        $scope.color=1;
                        var req ={
                            type:"GET",
                            uri:"getThirdCategory",
                            data:{
                                scId:sessionStorage.twoId
                            }
                        };
                        httpServices.getcourseCata(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.ThreeList = response.data;
                            })
                        });


                    }else if(sessionStorage.topId != undefined && sessionStorage.twoId != undefined && sessionStorage.thrId != undefined){
                        $scope.tab = sessionStorage.topId;
                        if(sessionStorage.topId == $scope.CityId){
                            $scope.showCOn=1;
                            $scope.currentId =  sessionStorage.twoId;//二级id
                            $scope.currentId3 = sessionStorage.thrId;//三级id
                            var req ={
                                type:"GET",
                                uri:"getThirdCategory",
                                data:{
                                    scId:sessionStorage.twoId
                                }
                            };
                            httpServices.getcourseCata(req).success(function(response){
                                $scope.$apply(function(){
                                    $scope.ThreeList = response.data;
                                })
                            });
                        }else{
                            $scope.showCOn2=1;
                            $scope.currentId2 =  sessionStorage.twoId;//二级id
                            $scope.currentId4 = sessionStorage.thrId;//三级id
                            var req ={
                                type:"GET",
                                uri:"getThirdCategory?scId="+sessionStorage.twoId,
                                data:{}
                            };
                            httpServices.getcourseCata(req).success(function(response){
                                $scope.$apply(function(){
                                    $scope.ThreeSwitchDate = response.data;
                                })
                            });

                        }
                        $scope.color=1;
                        var req ={
                            type:"GET",
                            uri:"getList",
                            data:{
                                pageNo:$scope.listParams.pageNo,
                                pageSize:$scope.listParams.pageSize,
                                tcId:sessionStorage.topId,
                                scId:sessionStorage.twoId,
                                thirdId:sessionStorage.thrId
                            }
                        };
                        httpServices.getcourse(req).success(function(response){
                            $scope.$apply(function(){
                                console.log(response.data.list[0].labelList)
                                $scope.courseList = response.data.list;
                                $scope.startLevel = response.data.list.collectionCount;
                                $scope.totalItem = response.data.Pagination.totalCount;
                                getListModel2();
                            })
                        });

                    }else{
	                	 if(sessionStorage.hasOwnProperty("searchKeyWord")){
					            $scope.listParams.keyWord = sessionStorage.searchKeyWord ;
					            var req ={
				                type:"GET",
				                uri:"getList",
				                data:{
				                	
				                    keyWord:$scope.listParams.keyWord,
				                    pageNo:$scope.listParams.pageNo,
				                    pageSize:$scope.listParams.pageSize
				                }
					            };
					            httpServices.getcourse(req).success(function(response){
					                $scope.$apply(function(){
					                    $scope.courseList = response.data.list;
					                    $scope.startLevel = response.data.list.collectionCount;
					                    $scope.totalItem = response.data.Pagination.totalCount;
					                   
					                })
					            });
	       				 }else{
	       				 	 	courseList();
	       				 }
                       
                    }
                })
            });
        }
        /*一级分类*/
        /*默认显示1的课程列表*/
        function courseList(){
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });
        }


        /*热门1推荐课程数据*/
        function hotCourse(){
            var req ={
                type:"GET",
                uri:"getHotCourse?pageNo=1&pageSize=6&all=false",
                data:{}
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    var data = response.data.list;
                    $scope.hotCouse = data;
                    var order = "visitCount";
                    $scope.col = order;//默认按name列排序
                    $scope.desc = 0;

                })
            });

        };
        /*最近浏览数据*/
        function recentData(){
            var req ={
                type:"GET",
                uri:"getBrowseCourse",
                data:{
                    pageNo:1,
                    pageSize:10,
                    all:"false"
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    if(response.data == null){

                    }else{
                        $scope.recentDate = response.data.list;
                    }

                })
            });
        }
        recentData();
        hotCourse();
        $scope.tabones=function(item,$index){
            $scope.ThreeList="";
            $scope.tab = $index;
            $scope.oneClassId = item.tcId;
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });


            var req ={
                type:"GET",
                uri:"getSecondaryCategory?tcId=" + item.tcId,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    var data = response.data.list;
                    $scope.WorkCousrse = data;
                })
            });

        }
        /*三级分类点击切换显示*/
        $scope.opThrennSwitch = function(item){

            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId!=undefined?$scope.oneClassId:"",
                    scId:$scope.twoClassId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });




            $scope.color=1;
            $scope.showCOn=1;
            $scope.currentId=$scope.twoClassId=item.scId;
            var req ={
                type:"GET",
                uri:"getThirdCategory?scId="+item.scId,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.ThreeList = response.data;
                })
            });
        };
        $scope.ThreeCourseSwitch=function(item){
            $scope.currentId3=item.dcId;
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId,
                    scId:$scope.twoClassId,
                    thirdId:item.dcId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });
        }
        /*点击全部加载默认列表*/
        /*排序*/
         $scope.isDisabled = false;
        $scope.peopecolor = false;
        $scope.DateColor = function(){
            $scope.isDisabled = !$scope.isDisabled;

            	var req ={
                type:"GET",
                uri:"getList",
                data:{
                	tcId:$scope.tab!=undefined?$scope.tab:"",
                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
	                  downloadCount:$scope.peopecolor,//下载排序
	                  uploadTime:$scope.isDisabled
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                   
                    getListModel();
                })
            });
           
          
        };
        
        $scope.peopleColor=function(){
     
            $scope.peopecolor = !$scope.peopecolor;

	             var req ={
	                type:"GET",
	                uri:"getList",
	                data:{
	                	tcId:$scope.tab!=undefined?$scope.tab:"",
	                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
	                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
	                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",
	                    pageNo:$scope.listParams.pageNo,
	                     pageSize:$scope.listParams.pageSize,
	                    downloadCount:$scope.peopecolor,//下载排序
	                    uploadTime:$scope.isDisabled
	                }
	            };
	            httpServices.getcourse(req).success(function(response){
	                $scope.$apply(function(){
	                    $scope.courseList = response.data.list;
	                    $scope.startLevel = response.data.list.collectionCount;
	                    $scope.totalItem = response.data.Pagination.totalCount;
	                    getListModel();
	                })
	            });
           
        }
        $scope.all=function(){
            $scope.tab=-1;
            $scope.showCOn=-1;
            $scope.showCOn2=-1;
            $scope.ThreeList="";
            $scope.ThreeSwitchDate="";
            sessionStorage.removeItem("topId")
            sessionStorage.removeItem("twoId")
            sessionStorage.removeItem("thrId")
            courseList();
            $scope.color = 2;
            $scope.currentId=0.1;
            $scope.currentId3 = 0.1;
            $scope.currentId2 = 0.1;
            $scope.currentId4 = 0.1;
        };

        $scope.saveSeachCOn=function(a){
            $scope.seacCon =a;
        }
        $scope.seachVIdeo=function(){
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    keyWord:$scope.seacCon,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel();
                })
            });

        }
        /*分页效果*/

        $scope.list = [];
        function getListModel(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                }
            };
        }

        function getListModel2(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                }
            };
        }
        function getListModel3(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    courseList();
                }
            };
        }



    })
});


